﻿<template>
  <el-container class="home_contaier">
<!--    头部区域-->

    <el-header>

      <div>
        <img  style="margin:10px;" src="../../assets/logo.webp" alt="">
        <span>小药店后台管理</span>
      </div>
      <el-button @click="exit" type="info">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px':'200px'">
        <div class="toggle-button" @click="toggleCllapse">
          |||
        </div>
<!--        侧边栏菜单区域-->
<!--    el-menu的active-text-color表示激活态的颜色-->
        <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409EFF"
            :unique-opened="true"
            :collapse="isCollapse"
            :collapse-transition="false"
            router
            :default-active="activePath"
        >
<!--一级菜单-->
          <el-submenu v-for="item in menulist" :key="item.id" :index="item.id +''" >
<!--            一级菜单模板区域-->
            <template slot="title">
<!--              图标-->
              <i :class="iconsObj[item.id]"></i>
<!--              文本-->
              <span>{{ item.authName }}</span>
            </template>
<!-- 二级菜单-->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/'+subItem.path)">
              <template slot="title">
                <!--              图标-->
                <i class="el-icon-menu"></i>
                <!--              文本-->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
          <!-- <el-menu-item index="2222">
          <el-menu-item index="/home/PersonalMag">
            <i class="el-icon-menu"></i>
            <span slot="title" @click="saveNavState('/subItem.path')">人事管理</span>
          </el-menu-item>
          <el-menu-item index="/home/System">
            <i class="el-icon-menu"></i>
            <span slot="title" @click="saveNavState('/System')">系统管理</span>
          </el-menu-item> -->
        </el-menu>

      </el-aside>
      <el-main>

        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import menu from '../../menu.json'
export default {
  name: "Home",
  data(){
    return{
      // 左侧菜单数据
        menulist:[],
    //  定义一个一级菜单icon的对象
      iconsObj:{
        '125':'iconfont icon-user',
        '103':'iconfont icon-tijikongjian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-danju',
        '145':'iconfont icon-baobiao',
        '145':'iconfont icon-baobiao',
        '145':'iconfont icon-baobiao'
      },
      isCollapse:false,
      // 被激活的链接地址
      activePath:''

    }
  },
  // 当home组件被创建出来时就应获取 左侧菜单
  created() {
    this.menulist=menu
    this.activePath=window.sessionStorage.getItem("activePath");
  },
  methods:{
    exit:function (){
      window.sessionStorage.clear();
      this.$router.push('/login')
    },
    
    // 点击按钮切换左侧菜单的折叠与展开
    toggleCllapse:function (){
        this.isCollapse=!this.isCollapse;
    },
    // 保存链接激活状态
    saveNavState(activePath){
      console.log(activePath,"-----");
      window.sessionStorage.setItem('activePath',activePath);
      this.activePath=activePath;
    }
  }
}
</script>

<style scoped lang="less">
.home_contaier{
  height: 100%;
}
.el-header{
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  // 纵向上居中对齐
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div{
    display: flex;
    align-items: center;
    span{
      margin-left: 15px;
    }
    img{
      width: 60px;
      height: 60px;
    }
    }
  }


.el-aside{
  background-color: #333744;
}
.el-menu{
  border-right: none;
}
.el-main{
  background-color:#E9EDF1;
}
.iconfont{
  margin-right: 10px;
}
.toggle-button{
  background-color:#4A5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>